package com.ruoyi.base.service.impl;

import java.util.List;
import java.util.UUID;

import com.ruoyi.base.domain.BasPollutionsource;
import com.ruoyi.base.mapper.BasPollutionsourceMapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.pwk.domain.BusPwkRhoutfall;
import com.ruoyi.pwk.mapper.BusPwkRhoutfallMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.base.mapper.BasePipenetworkassociationtableMapper;
import com.ruoyi.base.domain.BasePipenetworkassociationtable;
import com.ruoyi.base.service.IBasePipenetworkassociationtableService;
import org.springframework.util.Assert;

/**
 * 管网关联关系(企业关联排口污水处理厂关联入河排污口)Service业务层处理
 * 
 * @author maliang
 * @date 2025-10-01
 */
@Service
public class BasePipenetworkassociationtableServiceImpl implements IBasePipenetworkassociationtableService 
{
    @Autowired
    private BasePipenetworkassociationtableMapper basePipenetworkassociationtableMapper;
    @Autowired
    private BusPwkRhoutfallMapper busPwkRhoutfallMapper;
    @Autowired
    private BasPollutionsourceMapper basPollutionsourceMapper;

    /**
     * 查询管网关联关系(企业关联排口污水处理厂关联入河排污口)
     * 
     * @param id 管网关联关系(企业关联排口污水处理厂关联入河排污口)主键
     * @return 管网关联关系(企业关联排口污水处理厂关联入河排污口)
     */
    @Override
    public BasePipenetworkassociationtable selectBasePipenetworkassociationtableById(String id)
    {
        return basePipenetworkassociationtableMapper.selectBasePipenetworkassociationtableById(id);
    }

    /**
     * 查询管网关联关系(企业关联排口污水处理厂关联入河排污口)列表
     * 
     * @param basePipenetworkassociationtable 管网关联关系(企业关联排口污水处理厂关联入河排污口)
     * @return 管网关联关系(企业关联排口污水处理厂关联入河排污口)
     */
    @Override
    public List<BasePipenetworkassociationtable> selectBasePipenetworkassociationtableList(BasePipenetworkassociationtable basePipenetworkassociationtable)
    {
        return basePipenetworkassociationtableMapper.selectBasePipenetworkassociationtableList(basePipenetworkassociationtable);
    }

    /**
     * 新增管网关联关系(企业关联排口污水处理厂关联入河排污口)
     * 
     * @param basePipenetworkassociationtable 管网关联关系(企业关联排口污水处理厂关联入河排污口)
     * @return 结果
     */
    @Override
    public int insertBasePipenetworkassociationtable(BasePipenetworkassociationtable basePipenetworkassociationtable)
    {
        basePipenetworkassociationtable.setId(UUID.randomUUID().toString());
        Assert.notNull(basePipenetworkassociationtable.getWspolluteid(),"请选择企业");
        Assert.notNull(basePipenetworkassociationtable.getRhoutfallid(),"请选择排污口");
        BasPollutionsource basPollutionsource = basPollutionsourceMapper.selectBasPollutionsourceById(basePipenetworkassociationtable.getWspolluteid());
        BusPwkRhoutfall busPwkRhoutfall = busPwkRhoutfallMapper.selectBusPwkRhoutfallById(basePipenetworkassociationtable.getRhoutfallid());
        Assert.notNull(basPollutionsource,"企业不存在");
        Assert.notNull(busPwkRhoutfall,"排污口不存在");
        copyValue(basePipenetworkassociationtable,basPollutionsource,busPwkRhoutfall);
        return basePipenetworkassociationtableMapper.insertBasePipenetworkassociationtable(basePipenetworkassociationtable);
    }

    private void copyValue(BasePipenetworkassociationtable basePipenetworkassociationtable,BasPollutionsource basPollutionsource,BusPwkRhoutfall busPwkRhoutfall){
        basePipenetworkassociationtable.setWspollutename(basPollutionsource.getPollutename());
        basePipenetworkassociationtable.setRhoutfallname(busPwkRhoutfall.getOutfallname());
        if(StringUtils.isNotEmpty(basPollutionsource.getLongitude())){
            basePipenetworkassociationtable.setLongitude(Double.parseDouble(basPollutionsource.getLongitude()));
        }
        if(StringUtils.isNotEmpty(basPollutionsource.getLatitude())){
            basePipenetworkassociationtable.setLatitude(Double.parseDouble(basPollutionsource.getLatitude()));
        }
        basePipenetworkassociationtable.setRiverid(basPollutionsource.getRiverid());
        basePipenetworkassociationtable.setRivername(basPollutionsource.getRivername());
    }
    /**
     * 修改管网关联关系(企业关联排口污水处理厂关联入河排污口)
     * 
     * @param basePipenetworkassociationtable 管网关联关系(企业关联排口污水处理厂关联入河排污口)
     * @return 结果
     */
    @Override
    public int updateBasePipenetworkassociationtable(BasePipenetworkassociationtable basePipenetworkassociationtable)
    {
        Assert.notNull(basePipenetworkassociationtable.getWspolluteid(),"请选择企业");
        Assert.notNull(basePipenetworkassociationtable.getRhoutfallid(),"请选择排污口");
        BasPollutionsource basPollutionsource = basPollutionsourceMapper.selectBasPollutionsourceById(basePipenetworkassociationtable.getWspolluteid());
        BusPwkRhoutfall busPwkRhoutfall = busPwkRhoutfallMapper.selectBusPwkRhoutfallById(basePipenetworkassociationtable.getRhoutfallid());
        Assert.notNull(basPollutionsource,"企业不存在");
        Assert.notNull(busPwkRhoutfall,"排污口不存在");
        copyValue(basePipenetworkassociationtable,basPollutionsource,busPwkRhoutfall);
        return basePipenetworkassociationtableMapper.updateBasePipenetworkassociationtable(basePipenetworkassociationtable);
    }

    /**
     * 批量删除管网关联关系(企业关联排口污水处理厂关联入河排污口)
     * 
     * @param ids 需要删除的管网关联关系(企业关联排口污水处理厂关联入河排污口)主键
     * @return 结果
     */
    @Override
    public int deleteBasePipenetworkassociationtableByIds(String[] ids)
    {
        return basePipenetworkassociationtableMapper.deleteBasePipenetworkassociationtableByIds(ids);
    }

    /**
     * 删除管网关联关系(企业关联排口污水处理厂关联入河排污口)信息
     * 
     * @param id 管网关联关系(企业关联排口污水处理厂关联入河排污口)主键
     * @return 结果
     */
    @Override
    public int deleteBasePipenetworkassociationtableById(String id)
    {
        return basePipenetworkassociationtableMapper.deleteBasePipenetworkassociationtableById(id);
    }
}
